/*****
 * 
 * ExpansionListParser.java
 * 
 * Copyright (c) 2011 Rocco Nicosia / TwoBit MTG Toolkit. 
 * 
 * License information: http://www.gnu.org/licenses/agpl-3.0-standalone.html
 * 
 *****/

package twobitclient.utils;

import java.io.IOException;
import java.io.File;

import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;

import twobitclient.twobit.MagicCard;
import twobitclient.twobit.MagicCard.*;

/*****
 * 
 * Allows parsing of XML files containing card data. 
 * 
 *****/
public class ExpansionListParser extends DefaultHandler {
    
    private ArrayList<MagicCard> m_cards = new ArrayList<MagicCard>();
    private MagicCard tempCard;
    private String tempStr;
    private Attributes tempAttributes;
    
    /////
    // Main method for XML parsing / error catching. 
    /////
    public ArrayList<MagicCard> parseDocument(String file)
    {   
        SAXParserFactory spf = SAXParserFactory.newInstance();
        
        try {    
            SAXParser sp = spf.newSAXParser();
            sp.parse(new File(file).getAbsolutePath(), this);
        } catch (IOException ie) {
            System.out.println("Failed to open " + file);
            ie.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return m_cards;
    }
    
    @Override
    public void startElement(String uri, String localName, String qName, 
                        Attributes attributes) throws SAXException {
        
        if (qName.equals("card")) {
            tempCard = new MagicCard(attributes.getValue("name"),
                                Integer.parseInt(attributes.getValue("id")));
        }
        tempStr = "";
        tempAttributes = attributes;
    }
    
    @Override
    public void characters(char[] ch, int start, int length)
                    throws SAXException {
        
        String addend = new String(ch, start, length);
        tempStr += addend;
    }
    
    @Override
    public void endElement(String uri, String localName, String qName)
                    throws SAXException {
        
        if (qName.equals("supertype") && !tempStr.equals("")) {
            if (tempStr.equals("Basic")) {
                tempCard.setSuperType(SuperType.BASIC);
            } else if (tempStr.equals("Legendary")) {
                tempCard.setSuperType(SuperType.LEGENDARY);
            } else if (tempStr.equals("Snow")) {
                tempCard.setSuperType(SuperType.SNOW);
            } else if (tempStr.equals("Tribal")) {
                tempCard.setSuperType(SuperType.TRIBAL);
            } else if (tempStr.equals("World")) {
                tempCard.setSuperType(SuperType.WORLD);
            }
        }
        
        else if (qName.equals("type")) {
            if (tempStr.equals("Artifact")) {
                tempCard.setType(Type.ARTIFACT);
            } else if (tempStr.equals("Creature")) {
                tempCard.setType(Type.CREATURE);
            } else if (tempStr.equals("Enchantment")) {
                tempCard.setType(Type.ENCHANTMENT);
            } else if (tempStr.equals("Instant")) {
                tempCard.setType(Type.INSTANT);
            } else if (tempStr.equals("Land")) {
                tempCard.setType(Type.LAND);
            } else if (tempStr.equals("Planeswalker")) {
                tempCard.setType(Type.PLANESWALKER);
            } else {
                tempCard.setType(Type.SORCERY);
            }
        }
        
        else if (qName.equals("manatype")) {
            if (tempStr.equals("Colorless")) {
                tempCard.setManaType(ManaType.COLORLESS);
            } else if (tempStr.equals("White")) {
                tempCard.setManaType(ManaType.WHITE);
            } else if (tempStr.equals("Blue")) {
                tempCard.setManaType(ManaType.BLUE);
            } else if (tempStr.equals("Black")) {
                tempCard.setManaType(ManaType.BLACK);
            } else if (tempStr.equals("Red")) {
                tempCard.setManaType(ManaType.RED);
            } else {
                tempCard.setManaType(ManaType.GREEN);
            }
        }
        
        else if (qName.equals("subtype") && !tempStr.equals("")) {
            tempCard.addSubType(tempStr);
        }
        
        else if (qName.equals("base")) {
            String tempValue = tempAttributes.getValue("stat");
            
            if (tempValue.equals("power")) {
                tempCard.setPower(Integer.parseInt(tempStr));
            } else if (tempValue.equals("toughness")) {
                tempCard.setToughness(Integer.parseInt(tempStr));
            } else if (tempValue.equals("loyalty")) {
                tempCard.setLoyalty(Integer.parseInt(tempStr));
            }
        }
        
        else if (qName.equals("ability")) {
            tempCard.addAbility(tempStr);
        }
        
        else if (qName.equals("mana")) {
            String tempValue = tempAttributes.getValue("color");
            
            if (tempValue.equals("colorless")) {
                tempCard.addManaCost(
                            ManaType.COLORLESS, Integer.parseInt(tempStr));
            } else if (tempValue.equals("white")) {
                tempCard.addManaCost(
                            ManaType.WHITE, Integer.parseInt(tempStr));
            } else if (tempValue.equals("blue")) {
                tempCard.addManaCost(
                            ManaType.BLUE, Integer.parseInt(tempStr));
            } else if (tempValue.equals("black")) {
                tempCard.addManaCost(
                            ManaType.BLACK, Integer.parseInt(tempStr));
            } else if (tempValue.equals("red")) {
                tempCard.addManaCost(
                            ManaType.RED, Integer.parseInt(tempStr));
            } else {
                tempCard.addManaCost(
                            ManaType.GREEN, Integer.parseInt(tempStr));
            }
        }
        
        else if (qName.equals("battletext")) {
            tempCard.setBattleText(tempStr.replaceAll("\n", " "));
        }
        
        /*  Flavor text not implemented. Arguably unimportant. :P
        else if (qName.equals("flavortext")) {
            tempCard.setFlavorText(tempStr.replaceAll("\n", " "));
        } */
        
        else if (qName.equals("rarity")) {
            tempCard.setRarity(tempStr);
        }
        
        else if (qName.equals("artist")) {
            tempCard.setArtist(tempStr);
        }
        
        else if (qName.equals("card")) {
            m_cards.add(tempCard);
        }
    } 
}
